package com.chenfan.mcn.utils;

import lombok.SneakyThrows;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.apache.commons.lang3.time.DateUtils;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Author liuyang
 * @Date 2021/10/19
 * @Time 17:53
 * @Version V1.0
 **/
public class DateUtil {

    private static long dayMs = 24*60*60*1000;;


    /**
     * 获取当前月第一天
     *
     * @param month
     * @return
     */
    public static String getFirstDayOfMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最小天数
        int day = calendar.getActualMinimum(Calendar.DAY_OF_MONTH);
        // 设置日历中月份的最小天数
        calendar.set(Calendar.DAY_OF_MONTH, day);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime()) + " 00:00:00";
    }


    public static String getLastDayOfMonth(int month) {
        Calendar calendar = Calendar.getInstance();
        // 设置月份
        calendar.set(Calendar.MONTH, month - 1);
        // 获取某月最大天数
        int last = 0;
        //2月的平年瑞年天数
        final int twoMonth = 2;
        if (month == twoMonth) {
            last = calendar.getLeastMaximum(Calendar.DAY_OF_MONTH);
        } else {
            last = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        }
        // 设置日历中月份的最大天数
        calendar.set(Calendar.DAY_OF_MONTH, last);
        // 格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return sdf.format(calendar.getTime()) + " 23:59:59";
    }

    public static int getMonth(String month) {
        int value;
        String substring = month.substring(4, 5);
        System.out.println(substring);
        String substring1 = month.substring(4, 6);
        System.out.println(substring1);
        String substring2 = month.substring(5, 6);
        if (String.valueOf(0).equals(substring)) {
            value = Integer.parseInt(substring2);
        } else {
            value = Integer.parseInt(substring1);
        }
        return value;
    }


    public static String getYear(String month){
        String year = month.substring(0, 4);
        System.out.println(year);
        return year;
    }

    public static String startTime(String value){
        int month = getMonth(value);
        String firstDayOfMonth = getFirstDayOfMonth(month);
        String substring = firstDayOfMonth.substring(4, firstDayOfMonth.length());
        String year = getYear(value);
        String startTime = year + substring;
        System.out.println(startTime);
        return startTime;

    }

    public static String endTime(String value){
        int month = getMonth(value);
        String lastDayOfMonth = getLastDayOfMonth(month);
        String substring = lastDayOfMonth.substring(4, lastDayOfMonth.length());
        String year = getYear(value);
        String endTime = year + substring;
        System.out.println(endTime);
        return endTime;

    }

    @SneakyThrows
    public static Date getNowYearMonth() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        String yearMonth = sdf.format(new Date());
        return sdf.parse(yearMonth);
    }

    public static boolean isValid(Date startDate, Date endDate) {
        if(Objects.isNull(startDate) || Objects.isNull(endDate)) {
            return false;
        }
        Date now = new Date();
        return startDate.before(now) && endDate.after(now);
    }

    /**
     * 获取上个月日期
     * @param date
     * @return
     */
    public static Date getPreMonthDate(Date date) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        c.add(Calendar.MONTH, -1);
        return c.getTime();
    }

    /**
     * 获取日期月份最后一天
     * @param month
     * @return
     */
    public static int lastDayOfMonth(Date month) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(month);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        return cal.getActualMaximum(Calendar.DAY_OF_MONTH);
    }

    /**
     * 获取日期当月天数
     * @param date
     * @return
     */
    public static int dayOfMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        return cal.get(Calendar.DAY_OF_MONTH);
    }

    /**
     *
     * @param month
     * @return
     */
    public static List<Date> getAllDayOfMonth(Date month) {
        int lastDay = DateUtil.lastDayOfMonth(month);
        List<Date> dayList = new ArrayList(lastDay);
        for (int i = 1; i <= lastDay ; i++) {
            Calendar cal = Calendar.getInstance();
            cal.setTime(month);
            cal.set(Calendar.DAY_OF_MONTH, i);
            cal.set(Calendar.HOUR, 0);
            cal.set(Calendar.MINUTE, 0);
            cal.set(Calendar.SECOND, 0);
            cal.set(Calendar.MILLISECOND, 0);
            dayList.add(cal.getTime());
        }
        return dayList;
    }

    /**
     * 纠正日期格式
     * @param dateStr
     * @param format
     * @return
     */
    @SneakyThrows
    public static String correctDate(String dateStr, String format) {
        Date targetDate = DateUtils.parseDateStrictly(dateStr, Locale.CHINA,
                "yyyy-MM-dd HH:mm:ss",
                "yyyy-MM-dd",
                "yyyy/MM/dd HH:mm:ss",
                "yyyy/MM/dd", format);
        return DateFormatUtils.format(targetDate, format);
    }

    public static String convertToString(LocalDateTime dateTime,String format) {
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern(format);
        return dateTime.format(dateTimeFormatter);
    }

    public static Date getMonthFirstDay(Date date){
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMinimum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    public static Date getMonthLastDay(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.set(Calendar.DAY_OF_MONTH,calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        calendar.set(Calendar.HOUR_OF_DAY,0);
        calendar.set(Calendar.MINUTE,0);
        calendar.set(Calendar.SECOND,0);
        return calendar.getTime();
    }

    public static String getTimeStr(Date date,String pattern){
        if(date == null){
            return "";
        }
        return new SimpleDateFormat(pattern).format(date);
    }

    @SneakyThrows
    public static Date getLocalDate(Date date) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String s = sdf.format(date);
        return sdf.parse(s);
    }


    @SneakyThrows
    public static Date parseDate(String dateStr) {
        return DateUtils.parseDateStrictly(dateStr, Locale.CHINA,
                "yyyy-MM-dd HH:mm:ss",
                "yyyy-MM-dd",
                "yyyy/MM/dd HH:mm:ss");
    }

    @SneakyThrows
    public static long getPhaseDifference(String start, String end, DateFormat dft) {
        Date star = dft.parse(start);
        Date endDay = dft.parse(end);
        long num = endDay.getTime() - star.getTime();
        if(num > 0) {
            return Math.floorDiv(num, dayMs) + (Math.floorMod(num, dayMs) > 0L ? 1L : 0L);
        }
        return - (Math.floorDiv(-num, dayMs) + (Math.floorMod(-num, dayMs) > 0L ? 1L : 0L));
    }

    public static boolean contains(Date date, Date startDate, Date endDate) {
        return !date.before(startDate) && !date.after(endDate);
    }
}
